package com.goods.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.IHealthMapper;
import com.goods.business.mapper.IUserMapper;
import com.goods.business.service.IHealthService;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/7 - 17:49
 * @Author: Hyoung
 * @Description:
 */
@Service
public class HealthServiceImpl implements IHealthService {


    @Autowired
    private IHealthMapper iHealthMapper;

    @Autowired
    private IUserMapper iUserMapper;

    @Override
    public PageVO<HealthVO> history(Integer pageNum, Integer pageSize, String authorization) {
        PageHelper.startPage(pageNum, pageSize);
        Example exampleUser = new Example(User.class);
        String username = JWTUtils.getUsername(authorization);
        exampleUser.createCriteria().andEqualTo("username", username);
        User user = iUserMapper.selectOneByExample(exampleUser);

        Example exampleHealth = new Example(Health.class);
        exampleHealth.createCriteria().andEqualTo("userId", user.getId());

        List<Health> healthList = iHealthMapper.selectByExample(exampleHealth);
        List<HealthVO> collect = healthList.stream().map(health -> {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health, healthVO);
            return healthVO;
        }).collect(Collectors.toList());

        PageInfo<Health> healthPageInfo = new PageInfo<>(healthList);

        return new PageVO<>(healthPageInfo.getTotal(), collect);
    }

    @Override
    public void report(HealthVO healthVO, String authorization) {
        Example exampleUser = new Example(User.class);
        String username = JWTUtils.getUsername(authorization);
        exampleUser.createCriteria().andEqualTo("username", username);
        User user = iUserMapper.selectOneByExample(exampleUser);

        healthVO.setUserId(user.getId());
        healthVO.setCreateTime(new Date());
        Health health = new Health();
        BeanUtils.copyProperties(healthVO, health);
        iHealthMapper.insert(health);

    }

    @Override
    public HealthVO isReport(String authorization) {
        Example exampleUser = new Example(User.class);
        String username = JWTUtils.getUsername(authorization);
        exampleUser.createCriteria().andEqualTo("username", username);
        User user = iUserMapper.selectOneByExample(exampleUser);

        Example exampleHealth = new Example(Health.class);
        Date date = new Date();
        Date beginOfDay = DateUtil.beginOfDay(date);

        Date endOfDay = DateUtil.endOfDay(date);

        exampleHealth.createCriteria()
                .andEqualTo("userId", user.getId()).andBetween("createTime",
                        beginOfDay, endOfDay);
        //一天的开始，结果：2019-09-17 00:00:00


        Health health = iHealthMapper.selectOneByExample(exampleHealth);

        HealthVO healthVO = null;
        if (!Objects.isNull(health)) {
            healthVO = new HealthVO();
            BeanUtils.copyProperties(health, healthVO);
        }
//        iHealthMapper.sel
        return healthVO;
    }
}
